/*
 * Copyright 2007 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Sun designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Sun in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */
package im.composer.media.sound.synth;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.Soundbank;
import javax.sound.midi.spi.SoundbankReader;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.UnsupportedAudioFileException;

import im.composer.audio.AudioSystem;

/**
 * Soundbank reader that uses audio files as soundbanks.
 * 
 * @author Karl Helgason
 */
public class AudioFileSoundbankReader extends SoundbankReader {

	public Soundbank getSoundbank(URL url) throws InvalidMidiDataException, IOException {
		try {
			AudioInputStream ais = AudioSystem.getAudioInputStream(url);
			Soundbank sbk = getSoundbank(ais);
			ais.close();
			return sbk;
		} catch (UnsupportedAudioFileException e) {
			return null;
		} catch (IOException e) {
			return null;
		}
	}

	public Soundbank getSoundbank(InputStream stream) throws InvalidMidiDataException, IOException {
		stream.mark(512);
		try {
			AudioInputStream ais = AudioSystem.getAudioInputStream(stream);
			Soundbank sbk = getSoundbank(ais);
			if (sbk != null)
				return sbk;
		} catch (UnsupportedAudioFileException e) {
		} catch (IOException e) {
		}
		stream.reset();
		return null;
	}

	public Soundbank getSoundbank(AudioInputStream ais) throws InvalidMidiDataException, IOException {
		try {
			byte[] buffer;
			if (ais.getFrameLength() == -1) {
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				byte[] buff = new byte[1024 - (1024 % ais.getFormat().getFrameSize())];
				int ret;
				while ((ret = ais.read(buff)) != -1) {
					baos.write(buff, 0, ret);
				}
				ais.close();
				buffer = baos.toByteArray();
			} else {
				buffer = new byte[(int) (ais.getFrameLength() * ais.getFormat().getFrameSize())];
				new DataInputStream(ais).readFully(buffer);
			}
			ModelByteBufferWavetable osc = new ModelByteBufferWavetable(new ModelByteBuffer(buffer), ais.getFormat(), -4800);
			ModelPerformer performer = new ModelPerformer();
			performer.getOscillators().add(osc);

			SimpleSoundbank sbk = new SimpleSoundbank();
			SimpleInstrument ins = new SimpleInstrument();
			ins.add(performer);
			sbk.addInstrument(ins);
			return sbk;
		} catch (Exception e) {
			return null;
		}
	}

	public Soundbank getSoundbank(File file) throws InvalidMidiDataException, IOException {
		try {
			AudioInputStream ais = AudioSystem.getAudioInputStream(file);
			ais.close();
			ModelByteBufferWavetable osc = new ModelByteBufferWavetable(new ModelByteBuffer(file, 0, file.length()), -4800);
			ModelPerformer performer = new ModelPerformer();
			performer.getOscillators().add(osc);
			SimpleSoundbank sbk = new SimpleSoundbank();
			SimpleInstrument ins = new SimpleInstrument();
			ins.add(performer);
			sbk.addInstrument(ins);
			return sbk;
		} catch (UnsupportedAudioFileException e1) {
			return null;
		} catch (IOException e) {
			return null;
		}
	}
}
